package com.ztgf.shopping.service.audit;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.file.models.dto.FileInfoDTO;
import com.ztgf.file.models.service.IFileInfoService;
import com.ztgf.shopping.common.config.YmlConfig;
import com.ztgf.shopping.mapper.audit.AuditLogMapper;
import com.ztgf.shopping.models.bo.audit.AuditLogQueryBO;
import com.ztgf.shopping.models.dto.audit.AuditDTO;
import com.ztgf.shopping.models.dto.audit.AuditLogDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.audit.IAuditLogService;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.vo.audit.AuditLogVO;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.service.ISessionService;
import com.ztgf.sys.models.service.ISysUserService;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@DubboService
@Slf4j
public class AuditLogServiceImpl extends ServiceImpl<AuditLogMapper, AuditLogDTO> implements IAuditLogService {
    @DubboReference
    private IUserInfoService iUserInfoService;
    @DubboReference
    private ISysUserService iSysUserService;
    @Autowired
    @Lazy
    private IShopInfoService iShopInfoService;
    @Autowired
    private AuditServiceImpl auditService;
    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private ISessionService sessionService;
    @DubboReference
    private IFileInfoService fileInfoService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public Page<AuditLogVO> queryLogPageList(AuditLogQueryBO bo) {
        Page<AuditLogVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        ArrayList<AuditLogVO> auditLogVOArrayList = new ArrayList<AuditLogVO>();

        Wrapper<AuditLogDTO> wrapper = new EntityWrapper<>();
        if (bo.getAuditId() != null){
            wrapper.eq("audit_id", bo.getAuditId());
        }
        wrapper.orderBy("id", true);
        List<AuditLogVO> vos = baseMapper.queryLogPageList(page, bo);
        for (AuditLogVO vo : vos){
            AuditLogVO auditLogVO = new AuditLogVO();
            BeanUtils.copyProperties(vo, auditLogVO);
            UserInfoDTO dto = iUserInfoService.selectById(vo.getCreateUserId());
            if (dto != null){
                auditLogVO.setUserName(dto.getContactName());
            }else {
                SysUserDTO sysUserDTO = iSysUserService.selectById(vo.getCreateUserId());
                if (sysUserDTO != null){
                    auditLogVO.setUserName(sysUserDTO.getName());
                }
                else {
                    auditLogVO.setUserName("");
                }
            }

            auditLogVOArrayList.add(auditLogVO);
        }
        page.setRecords(auditLogVOArrayList);
        return page;
    }

    @Override
    public AuditLogDTO getByAidAndStatus(Long auditId, Integer status) {
        Wrapper<AuditLogDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("audit_id", auditId);
        wrapper.eq("status", status);
        wrapper.orderBy("create_time", false);
        List<AuditLogDTO> dtos = this.selectList(wrapper);
        if (dtos.size() >0){
            return dtos.get(0);
        }else {
            return null;
        }
    }

    @Override
    public boolean add(Long auditId, Integer stattus) {
        AuditDTO auditDTO = auditService.selectById(auditId);
        AuditLogDTO auditLogDTO = new AuditLogDTO();
        Long id = ymlConfig.getPkId();
        auditLogDTO.setId(id);

        Long userId = null;
        Object object = redisUtil.getSession (CommonParameter.getHeadParamsBO ().getSessionId (), CommonParameter.getHeadParamsBO ().getSource ().toString ());
        if(object instanceof UserInfoDTO){
            userId = ((UserInfoDTO) object).getId ();
        } if(object instanceof SysUserDTO){
            userId = ((SysUserDTO) object).getId ();
        }
        auditLogDTO.setCreateUserId(userId);
        auditLogDTO.setParams(auditDTO.getParams());
        auditLogDTO.setUserId(auditDTO.getUserId());
        auditLogDTO.setName(auditDTO.getName());
        auditLogDTO.setShopName(auditDTO.getShopName());
        auditLogDTO.setType(auditDTO.getType());
        auditLogDTO.setStatus(stattus);
        auditLogDTO.setAuditId(auditDTO.getId());
        auditLogDTO.setShopId(auditDTO.getShopId());
        auditLogDTO.setContent(auditDTO.getContent());
        return this.insert(auditLogDTO);
    }

    @Override
    public boolean updateOpt(Long auditId) {
        AuditDTO auditDTO = auditService.selectById(auditId);
        if (auditDTO.getStatus().equals(ShoppingCodeEnum.AUDIT_PASS.getCode())){
            AuditLogDTO auditLogDTO = new AuditLogDTO();
            AuditLogDTO oldAuditLogDTO = getByAidAndStatus(auditId, auditDTO.getStatus());
            auditLogDTO.setId(oldAuditLogDTO.getId());
            auditLogDTO.setOptStatus(1);
            return this.updateById(auditLogDTO);
        }else {
            return add(auditId, auditDTO.getStatus());
        }
    }

    @Override
    public List<AuditLogVO> queryList(Long auditId) {
        ArrayList<AuditLogVO> voList = new ArrayList<AuditLogVO>();

        Wrapper<AuditLogDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("audit_id", auditId);
        wrapper.orderBy("id", true);
        List<AuditLogDTO> dtoList = baseMapper.selectList(wrapper);
        for (AuditLogDTO dto : dtoList){
            AuditLogVO auditLogVO = new AuditLogVO();
            dto2vo(dto, auditLogVO);
            voList.add(auditLogVO);
        }
        return voList;
    }

    @Override
    public AuditLogVO get(Long id) {
        AuditLogVO auditLogVO = new AuditLogVO();
        AuditLogDTO dto = baseMapper.selectById(id);
        if(dto == null){
            dto = new AuditLogDTO();
            return auditLogVO;
        }
        dto2vo(dto, auditLogVO);

        return auditLogVO;
    }

    private void dto2vo(AuditLogDTO dto, AuditLogVO vo){
        BeanUtils.copyProperties(dto, vo);
        UserInfoDTO userInfoDTO = iUserInfoService.selectById(dto.getCreateUserId());
        if (userInfoDTO != null){
            vo.setUserName(userInfoDTO.getContactName());
        }else {
            SysUserDTO sysUserDTO = iSysUserService.selectById(dto.getCreateUserId());
            if (sysUserDTO != null){
                vo.setUserName(sysUserDTO.getName());
            }
            else {
                vo.setUserName("");
            }
        }
        ShopInfoDTO shopInfoDTO = iShopInfoService.selectById (dto.getShopId ());
        if (shopInfoDTO != null){
            vo.setShopName (shopInfoDTO.getShopName ());
            vo.setCompanyName (shopInfoDTO.getName ());
        }

    }
}
